import LicenseService from '../services/license'

class LicenseController {
    // 创建配置
    static async create(ctx: any) {
        const { license, time, phoneNumber } = ctx.request.body
        const result = await LicenseService.create(
            { license, time, phoneNumber },
            ctx.state.user.openid
        )

        ctx.body = {
            result
        }
    }

    // 更新配置
    static async update(ctx: any) {
        const { id, license, time, phoneNumber } = ctx.request.body
        const result = await LicenseService.update(
            { id, license, time, phoneNumber },
            ctx.state.user.openid
        )
        ctx.body = {
            result
        }
    }

    // 删除配置
    static async delete(ctx: any) {
        const { license } = ctx.request.body
        const result = await LicenseService.delete(license)

        ctx.body = {
            result
        }
    }

    // 查询配置
    static async query(ctx: any) {
        const { license } = ctx.request.body
        const result = await LicenseService.findOne(license)

        ctx.body = {
            result
        }
    }

    // 查询配置列表
    static async list(ctx: any) {
        const result = await LicenseService.findAll({
            ...ctx.request.body
        })

        ctx.body = {
            result
        }
    }

    // 查询配置列表
    static async userList(ctx: any) {
        const result = await LicenseService.findAll({
            openid: ctx.state.user.openid
        })

        ctx.body = {
            result
        }
    }
}

export default LicenseController
